<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button id="btn1">连接</button>
    <button id="btn2" disabled="true">发生数据</button> <br>
    发送的数据如下 <br>
    <span id="rec"></span>
    <script>
        var btn1 = document.querySelector("#btn1")
        var btn2 = document.querySelector("#btn2")
        var rec  = document.querySelector("#rec")
        let ws = null
        btn1.onclick=function(){
              ws = new WebSocket("ws://localhost:8889")
              ws.onopen = ()=>{
                console.log("连接成功");
                btn2.disabled=false
              }
              ws.onclose = ()=>{
                  console.log("连接服务器失败");
              }
              ws.onmessage = (msg)=>{
                 console.log("收到数据了");
                  rec.innerHTML = msg.data
              }
        }
        btn2.onclick = function(){
            ws.send("web send java")
        }
    </script>
</body>
</html>